package com.example.handl;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";

    /*Handler是android adk 处理异步消息的核心类，子线程与主线程通过Handler进行通信，
     * 子线程通过Handler来通知主线程来进行UI的更新操作*/
    Handler mainHandler, workHandler;
    HandlerThread mHandlerThread;
    TextView text;
    Button button1, button2, button3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text = findViewById(R.id.text1);

        // 创建这个handler的时候会和当当前的线程绑定
        mainHandler = new Handler();

        // 创建handlerthread对象
        mHandlerThread = new HandlerThread("handlerthread"){

        };

        mHandlerThread.start();


        // 创建一个工作线程，接收消息，处理消息
        workHandler = new Handler(mHandlerThread.getLooper()) {
            @Override
            public void handleMessage(Message msg) {
                Log.d(TAG, "handleMessage: " + Thread.currentThread().getName());
                switch (msg.what) {
                    case 1:
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        Log.d(TAG, "handleMessage: " + Thread.currentThread().getName());
                        // 通过主线程的post进行ui的更新，
                        mainHandler.post(new Runnable() {
                            @Override
                            public void run() {
                                text.setText("睡眠1秒");
                            }
                        });
                        break;
                    case 2:
                        try {
                            Thread.sleep(3000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        Log.d(TAG, "handleMessage: " + Thread.currentThread().getName());
                        mainHandler.post(new Runnable() {
                            @Override
                            public void run() {
                                text.setText("睡眠3秒");
                            }
                        });
                        break;
                    default:
                        break;
                }
            }
        };
        button1 = findViewById(R.id.button1);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Message message = Message.obtain();
                message.what = 1;
                message.obj = "B";
                workHandler.sendMessage(message);
            }
        });
        // 点击Button2
        button2 = (Button) findViewById(R.id.button2);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                // 通过sendMessage（）发送
                // a. 定义要发送的消息
                Message msg = Message.obtain();
                msg.what = 2; //消息的标识
                msg.obj = "B"; // 消息的存放
                // b. 通过Handler发送消息到其绑定的消息队列
                workHandler.sendMessage(msg);
            }
        });
        // 点击Button3退出消息循环
        button3 = findViewById(R.id.button3);
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mHandlerThread.quit();
            }
        });
    }
}
